package cn.mdmm.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class SendThread extends Thread {
	Socket socket;

	public SendThread() {
		super();
	}

	public SendThread(Socket socket) {
		super();
		this.socket = socket;
	}

	@Override
	public void run() {
		try {
			boolean flag = true;
			while (flag) {
				System.out.print("\n请输入: \t");
				// 2、获取输出流，向服务器端发送信息
				// 向本机的52000端口发出客户请求

				String readline;
				BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
				readline = br.readLine(); // 从系统标准输入读入一字符串
				if (!readline.equals("end")) {
					// 由系统标准输入设备构造BufferedReader对象
					PrintWriter write = new PrintWriter(socket.getOutputStream());
					// 由Socket对象得到输出流，并构造PrintWriter对象
					
					// 若从标准输入读入的字符串为 "end"则停止循环
					write.println(readline);
					// 将从系统标准输入读入的字符串输出到Server
					write.flush();
					// 刷新输出流，使Server马上收到该字符串
					System.out.println("\nClient:" + readline);
				} else {
					flag = false;
				}

			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
